/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.keepalive.util;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.logos.common.R;
import com.logos.common.base.BaseActivity;

/**
 * Created by hk on 2017/11/28.
 * 流氓通知栏的util 调用后通知拦会常驻，使用时需要配置58行及89行的应用图标 建议此类放在项目之中
 * 106行请配置点击通知栏后想要跳转的页面 根据实际需求 也可以取消配置 55行及87行
 */

public class NotificationUtil {

    public static final int NOTIFICATION_ID_BASE = 0x0003;

    private static final String NOTIFICATION_CHANNEL_ID_BASE = "zhongke_notification";

    /**
     * ??
     *
     * @param id      *
     * @param context *
     */
    public static void notify(int id, Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notifyO(id, NOTIFICATION_CHANNEL_ID_BASE, context);
        } else {
            notifyNormal(id, NOTIFICATION_CHANNEL_ID_BASE, context);
        }
    }

    private static void notifyNormal(int id, String channelId, Context context) {
        NotificationManager notificationManager
                = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
        //先设定RemoteViews
        builder.setContentTitle(context.getString(R.string.app_name))
                .setContentText("正在上传经纬度信息")
                .setContentIntent(getDefaultIntent(3, context)) //设置通知栏点击意图
                .setPriority(Notification.PRIORITY_HIGH) //设置该通知优先级
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON

        Notification notification = builder.build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification.visibility = Notification.VISIBILITY_PUBLIC;
        }
        if (notificationManager != null) {
            notificationManager.notify(id, notification);
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private static void notifyO(int id, String channelId, Context context) {
        NotificationManager notificationManager
                = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationChannel channel
                = new NotificationChannel(channelId,
                context.getString(R.string.app_name),
                NotificationManager.IMPORTANCE_HIGH);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }

        Notification.Builder builder = new Notification.Builder(context, channelId); //与channelId对应
        //先设定RemoteViews
        builder.setContentTitle(context.getString(R.string.app_name))
                .setContentText("正在上传经纬度信息")
                .setContentIntent(getDefaultIntent(3, context)) //设置通知栏点击意图
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON

        Notification notification = builder.build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.visibility = Notification.VISIBILITY_PUBLIC;
        if (notificationManager != null) {
            notificationManager.notify(id, notification);
        }
    }

    /**
     * 此方法为点击通知拦时想要跳转至哪个页面 根据实际需求进行配置
     * @param requestCode *
     * @param context *
     * @return *
     */
    private static PendingIntent getDefaultIntent(int requestCode, Context context) {
        Intent intent = new Intent(context, BaseActivity.class);
        return PendingIntent.getActivity(context,
                requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    /**
     * 取消通知栏
     *
     * @param id      *
     * @param context *
     */
    public static void cancel(int id, Context context) {
        NotificationManager notificationManager
                = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.cancel(id);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager != null) {
                notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID_BASE);
            }
        }
    }

}
